package com.acertificate.image;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * Example how to use multipart/form encoded POST request.
 */
public class SEAStorageUpload {

	HttpPost httppost = new HttpPost("http://1.abreact.sinaapp.com/sto.php");

	public static void main(String[] args) {
		SEAStorageUpload c = new SEAStorageUpload();
		UploadingThread t1 = c.new UploadingThread();
		UploadingThread1 t11 = c.new UploadingThread1();
		t1.start();
		t11.start();
	}

	class UploadingThread extends Thread {

		public void run() {
			HttpClient httpclient = new DefaultHttpClient();
			try {

				File dir = new File("F:/Photos/www.166122.com/");
				File[] imgs = dir.listFiles();
				for (File file : imgs) {
					System.out.println("------------------------------------------------------------------------------------------");
					FileBody bin = new FileBody(new File(file.getAbsolutePath()));
					StringBody comment = new StringBody(file.getName());

					MultipartEntity reqEntity = new MultipartEntity();
					reqEntity.addPart("file", bin);
					reqEntity.addPart("fileName", comment);
					httppost.setEntity(reqEntity);
					HttpResponse response = httpclient.execute(httppost);
					HttpEntity resEntity = response.getEntity();

					System.out.println(response.getStatusLine());
					if (resEntity != null) {
						if ("SUCCESS".equals(IOUtils.toString(resEntity.getContent()))) {
							if (file.delete()) {
								System.out.println(file.getAbsolutePath() + "\tUPLOAD & DELETE SUCCESS");
							}
						}
					}
					EntityUtils.consume(resEntity);
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					httpclient.getConnectionManager().shutdown();
				} catch (Exception ignore) {
				}
			}
		}

	}
	
	class UploadingThread1 extends Thread {

		public void run() {
			HttpClient httpclient = new DefaultHttpClient();
			try {

				File dir = new File("F:/Photos/www.mmlin.com/");
				File[] imgs = dir.listFiles();
				for (File file : imgs) {
					System.out.println("------------------------------------------------------------------------------------------");
					FileBody bin = new FileBody(new File(file.getAbsolutePath()));
					StringBody comment = new StringBody(file.getName());

					MultipartEntity reqEntity = new MultipartEntity();
					reqEntity.addPart("file", bin);
					reqEntity.addPart("fileName", comment);
					httppost.setEntity(reqEntity);
					HttpResponse response = httpclient.execute(httppost);
					HttpEntity resEntity = response.getEntity();

					System.out.println(response.getStatusLine());
					if (resEntity != null) {
						if ("SUCCESS".equals(IOUtils.toString(resEntity.getContent()))) {
							if (file.delete()) {
								System.out.println(file.getAbsolutePath() + "\tUPLOAD & DELETE SUCCESS");
							}
						}
					}
					EntityUtils.consume(resEntity);
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					httpclient.getConnectionManager().shutdown();
				} catch (Exception ignore) {
				}
			}
		}

	}
}
